package com.chery.media.ui.main

import android.annotation.SuppressLint
import android.app.DownloadManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Environment
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.Utils
import com.chery.media.BuildConfig
import com.chery.media.R
import com.chery.media.library.base.BaseViewModel
import com.chery.media.library.event.SingleLiveEvent
import com.chery.media.library.extend.asResponse
import com.chery.media.library.extend.toDrawable
import com.chery.media.library.extend.toJson
import com.chery.media.library.extend.toResString
import com.chery.media.library.extend.toast
import com.chery.media.library.network.ResponseThrowable
import com.chery.media.network.model.AppInfoBean
import com.chery.media.service.DownloadService
import com.chery.media.utils.AppInfoUtils
import com.chery.media.utils.appRepository
import kotlinx.coroutines.flow.catch


class MainViewModel: BaseViewModel(){

    val appInstallList = SingleLiveEvent<ArrayList<AppInfoBean>>()

    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        getVersion()
    }

    fun getAllLauncherIconPackages(context: Context){
        val appList  = AppInfoUtils(context).appList
        val launcherApp = arrayListOf<AppInfoBean>()
        if (appList.filter { it.packageName == "ru.yandex.yandexmaps" }.toList().isNotEmpty()) {
            val appInfoBean = AppInfoBean(R.string.yandex_map.toResString(), "ru.yandex.yandexmaps", R.drawable.ic_yandex_map.toDrawable())
            launcherApp.add(appInfoBean)
        }
        if (appList.filter { it.packageName == "com.hms.carplay" }.toList().isNotEmpty()) {
            val appInfoBean = AppInfoBean(R.string.car_play.toResString(), "com.hms.carplay", R.drawable.ic_car_play.toDrawable())
            launcherApp.add(appInfoBean)
        }
        if (appList.filter { it.packageName == "com.tima.carnet.vt" }.toList().isNotEmpty()) {
            val appInfoBean = AppInfoBean(R.string.android_auto.toResString(), "com.tima.carnet.vt", R.drawable.ic_android_auto.toDrawable())
            launcherApp.add(appInfoBean)
        }
//        if (appList.filter { it.packageName == "com.acloud.stub.localradio" }.toList().isNotEmpty()) {
//            val appInfoBean = AppInfoBean(R.string.radio.toResString(), "com.acloud.stub.localradio", R.drawable.ic_radio.toDrawable())
//            launcherApp.add(appInfoBean)
//        }
//        if (appList.filter { it.packageName == "com.acloud.stub.localradio" }.toList().isNotEmpty()) {
//        val appInfoBean = AppInfoBean(R.string.radio.toResString(), "com.acloud.stub.localradio", R.drawable.ic_radio.toDrawable())
//        launcherApp.add(appInfoBean)
//    }
        if (appList.filter { it.appName == "收音机" || it.appName == "Radio" || it.appName == "Радио" || it.appName == "Radyo" }.toList().isNotEmpty()) {
            val list = appList.filter { it.appName == "收音机" || it.appName == "Radio" || it.appName == "Радио" || it.appName == "Radyo" }.toList()
            val appInfoBean = AppInfoBean(R.string.radio.toResString(), list[0].packageName, R.drawable.ic_radio.toDrawable())
            launcherApp.add(appInfoBean)
        }
        launcherApp.add(AppInfoBean(R.string.music.toResString(), "", R.drawable.ic_music.toDrawable()))
        launcherApp.add(AppInfoBean(R.string.setting.toResString(), "", R.drawable.ic_setting.toDrawable()))
        launcherApp.addAll(appList.filter { it.appName != "chery" && it.appName != "收音机" && it.appName != "Radio" && it.appName != "Радио" && it.appName != "Radyo" && it.packageName != "com.tima.carnet.vt" && it.packageName != "ru.yandex.yandexmaps" }.toList())
        appInstallList.postValue(launcherApp)
    }

    /**
     * 获取版本信息
     */
    private fun getVersion(){
        launch {
            appRepository.getVersionData()
                .asResponse()
                .catch { if (it is ResponseThrowable) it.errMsg.toast() }
                .collect {
                    if(it.versionCode > BuildConfig.VERSION_CODE) {
                        startDownloadApk(it.downloadUrl)
//                        startDownloadApk("https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk")
                    }
                }
        }
    }

    @SuppressLint("Range")
    private fun startDownloadApk(url: String){
        val serviceIntent = Intent(Utils.getApp(), DownloadService::class.java)
        serviceIntent.putExtra("downloadUrl", url) // 传递需要下载的文件URL
        Utils.getApp().startService(serviceIntent) // 启动服务
    }

}
